/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package visualcbir.board;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author robertopinho
 */
public class TagImageCell extends AbstractCell{

    String fileName;
    List<String> tags;
    public BufferedImage image=null;
    
    
    
    public TagImageCell(String fileName,List<String> tags){
        this.fileName = fileName;
        this.tags = tags;
        Collections.sort(this.tags);
        makeTagImage();
    }
    
    
    public Double getDistance(CellItemInterface other) {
        int inter=0;
        int union=0;
        TagImageCell tagImage = (TagImageCell)other;
        for(String tag : tagImage.tags){
            if(this.tags.contains(tag)){
                inter++;
            }
        }
        union = tags.size()+tagImage.tags.size()-inter;
        
        
        return 1.0-(1.0*inter)/union;
    }

    
    public String getLabel() {
        return fileName;
    }

    public String getClassLabel() {
        if(tags.size()>0)
            return tags.get(0);
        else return "null vector";
    }

    public String getContentString() {
        StringBuffer sB = new StringBuffer();
        for(String tag : tags){
            sB.append(tag+" ");
        }
        return sB.toString();
    }
//"C:/Users/RobertoPinho/Documents/edu_what/research_what/testFiles_what/DocBoard Extra Files/runDocBoardSmall/runDocBoardSmall/groundtruth/""
 public static String imageCollectionRoot = "/home/sheila/NetBeansProjects/incBoardTese/groundtruth/";
 public void makeTagImage(){
        
     
            if(this.image!=null) return;

            File imageFile = new File(imageCollectionRoot + getLabel() + ".jpg");

            

            if (!imageFile.exists()) {
                imageFile = new File(imageCollectionRoot + getLabel() + ".gif");
            }

            if (!imageFile.exists()) {
                imageFile = new File(imageCollectionRoot + getLabel() + ".JPG");
            }


            if (!imageFile.exists()) {
                System.err.println(getLabel());
            }


            
            int w = 30;
            int h = 30;
            
            
            BufferedImage origImage = null;
            
            
            try {
            origImage = ImageIO.read(imageFile);
            Image scaledImage = origImage.getScaledInstance(w, h, Image.SCALE_AREA_AVERAGING);
            
            this.image = new BufferedImage(w, h, BufferedImage.OPAQUE);
            Graphics2D g2 = image.createGraphics();
            g2.drawImage(scaledImage, 0, 0, null);
            g2.dispose();
        
            //exportImage();
            
        } catch (IOException ex) {
            Logger.getLogger(TagImageCell.class.getName()).log(Level.SEVERE, null, ex);
        }
             }

    private void exportImage() {
        
        String dir = getLabel().split("/")[0];
        File path = new File("./smallimg/" + dir);
        
        if(!path.exists()){
            path.mkdir();
        }
        
        try {
            File imageFile = new File("./smallimg/" + getLabel() + ".jpg");
            ImageIO.write(this.image, "gif", imageFile);
        } catch (IOException ex) {
            Logger.getLogger(TagImageCell.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public double[] getVector() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getDataScalar() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String toXML() {
        String out =  fileName;
        for(String tag:tags){
            out+= tag;
        }
        return out;
    }

    
    
}
